Infinite looping in JS without growing call stack

We have a function (render) that returns a promise when done, that we want to run every 20ms.

The next call only starts when the previous function call is done.

A setInterval of 20ms does not work here because it ignores the run duration of the previous call.

Solution one using setTimeout sequence.

When the function resolves it starts a timeout that then runs the function again after 20ms.

This should make call stack grow forever

Start the setTimeout sequence, then go to Inspect > Sources > index.js and add a breakpoint in loopSetTimeoutSequence and look at the call stack size

Iterations count:

Heap size:

Solution two using constant setInterval of one tick

An interval is always running every tick. When the function resolves, the time is saved, if 20ms have passed since the last run, the function is called again

This should NOT make call stack grow forever

Iterations count:

Heap size: